package com.oauth.google.client.ui.orkut.tabs;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.components.client.ui.gadgets.LoadingPanel;
import com.oauth.google.client.beans.orkut.OrkutScrap;
import com.oauth.google.client.beans.orkut.OrkutScrapDetails;
import com.oauth.google.client.rpc.orkut.OrkutRemoteServiceAsync;
import com.oauth.google.client.ui.orkut.ConnectionPanel;

public class ScrapsTab extends Composite {

	private static ScrapsTabUiBinder uiBinder = GWT.create(ScrapsTabUiBinder.class);

	interface ScrapsTabUiBinder extends UiBinder<FlowPanel, ScrapsTab> {
	}

	@UiField
	VerticalPanel scrapsPanel;

	@UiField
	LoadingPanel loadingPanel;

	private OrkutRemoteServiceAsync service;

	public ScrapsTab(OrkutRemoteServiceAsync pService) {
		service = pService;
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void init() {
		loadingPanel.setVisible(true);

		scrapsPanel.clear();

		service.getOrkutScraps(new OAuthAsnyCallBack<OrkutScrapDetails>() {

			@Override
			public void onSuccess(OrkutScrapDetails pResult) {
				scrapsPanel.clear();
				if (pResult != null && pResult.getScraps().isEmpty()) {
					scrapsPanel.add(new Label("You don't have any scraps"));
				} else {
					for (OrkutScrap user : pResult.getScraps()) {
						scrapsPanel.add(new ConnectionPanel(user));
					}
				}
				loadingPanel.setVisible(false);
			}
		});
	}
}
